//
//  ALStatusRetweetView.swift
//  Swift01
//
//  Created by Miaoyang on 16/4/15.
//  Copyright © 2016年 Miaoyang. All rights reserved.
//

import UIKit



class ALStatusRetweetView: UIView {
    
    /// 被转发的微博
    var retweetStatus: ALStatus? {
        
        didSet {
            // 当别人设置被转发微博的内容,我们来设置相应的内容到view上面
            // 被转发的微博文本内容 = @被转发微博的用户名称: 被转发微博的文本内容
            let retweetName = retweetStatus?.user?.screen_name ?? "被转发微博的用户名称"
            let retweetText = retweetStatus?.text ?? "被转发微博内容为空"
            
            let retweetContent = "@\(retweetName): \(retweetText)"
            retweetLabel.text = retweetContent
            
            // 设置配图
            retweetPictureView.pictureURLs = retweetStatus?.storePicutreURLs
            
            // 当没有图片的时候,HMStatusPictureView距离retweetLabel的底部不需要8的间距
            // snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus?.storePicutreURLs == nil ? 0 : ALStatusCellMargin
                make.top.equalTo(retweetLabel.snp_bottom).offset(margin)
            }
        }
    }
    

 
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
       
        
    }
    //MARK:设置界面
    private func prepareUI(){
        //添加控件
        
        // 1.添加控件
        addSubview(bkgButton)
        addSubview(retweetLabel)
        addSubview(retweetPictureView)
        
        //设置约束
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        // 被转发的微博文本内容
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(ALStatusCellMargin)
            make.trailing.equalTo(self).offset(-ALStatusCellMargin)
            
            // 配图
            retweetPictureView.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(self).offset(ALStatusCellMargin)
                make.top.equalTo(retweetLabel.snp_bottom).offset(ALStatusCellMargin)
                
                // 配图大小当别人给他设置pictureURL后他会自动更新,不用我们操心
                make.size.equalTo(CGSizeZero)
            }
            
            // 添加底部约束让被转发的view的底部参照被转发的配图视图
            self.snp_makeConstraints { (make) -> Void in
                make.bottom.equalTo(retweetPictureView).offset(ALStatusCellMargin)
            }
    }
    }
    //MARK:懒加载控件
    //背景按钮
     lazy var bkgButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    //被转发的文字
     lazy var retweetLabel:UILabel = {
        
        let label = UILabel(fontSize: ALStatusContentFontSize, textColor: UIColor.darkGrayColor())
        //自动换行
        label.numberOfLines = 0
        return label
        
    }()
    
    /// 被转发的配图视图
     lazy var retweetPictureView: ALStatusPictureView = ALStatusPictureView()


}
